package com.plugin.manage.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.plugin.core.model.entity.SysMenu;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 系统菜单Mapper接口
 * 
 * @author mark
 * @date 2025-07-28
 */
@Mapper
public interface SysMenuMapper extends BaseMapper<SysMenu> {

    /**
     * 根据菜单ID查询菜单
     * 
     * @param menuId 菜单ID
     * @return 菜单信息
     */
    @Select("SELECT * FROM sys_menu WHERE menu_id = #{menuId}")
    SysMenu selectByMenuId(@Param("menuId") Long menuId);

    /**
     * 根据角色ID查询菜单权限
     * 
     * @param roleId 角色ID
     * @return 菜单列表
     */
    @Select("SELECT m.* FROM sys_menu m " +
            "INNER JOIN sys_permission p ON m.menu_id = p.resource_id " +
            "WHERE p.role_id = #{roleId} " +
            "ORDER BY m.parent_id, m.order_num")
    List<SysMenu> selectMenusByRoleId(@Param("roleId") Long roleId);

    /**
     * 查询所有菜单
     * 
     * @return 菜单列表
     */
    @Select("SELECT * FROM sys_menu ORDER BY parent_id, order_num")
    List<SysMenu> selectAllMenus();

    /**
     * 根据父菜单ID查询子菜单
     * 
     * @param parentId 父菜单ID
     * @return 子菜单列表
     */
    @Select("SELECT * FROM sys_menu WHERE parent_id = #{parentId} ORDER BY order_num")
    List<SysMenu> selectMenusByParentId(@Param("parentId") Long parentId);

    /**
     * 根据菜单类型查询菜单
     * 
     * @param menuType 菜单类型
     * @return 菜单列表
     */
    @Select("SELECT * FROM sys_menu WHERE menu_type = #{menuType} ORDER BY parent_id, order_num")
    List<SysMenu> selectMenusByType(@Param("menuType") String menuType);

    /**
     * 根据系统类型查询菜单
     * 
     * @param system 系统类型
     * @return 菜单列表
     */
    @Select("SELECT * FROM sys_menu WHERE system = #{system} ORDER BY parent_id, order_num")
    List<SysMenu> selectMenusBySystem(@Param("system") Integer system);

    /**
     * 查询正常状态的菜单
     * 
     * @return 菜单列表
     */
    @Select("SELECT * FROM sys_menu WHERE status = '0' AND visible = '0' ORDER BY parent_id, order_num")
    List<SysMenu> selectActiveMenus();

    /**
     * 根据权限标识查询菜单
     * 
     * @param perms 权限标识
     * @return 菜单信息
     */
    @Select("SELECT * FROM sys_menu WHERE perms = #{perms}")
    SysMenu selectMenuByPerms(@Param("perms") String perms);

    /**
     * 检查菜单名称是否存在
     * 
     * @param menuName 菜单名称
     * @param parentId 父菜单ID
     * @param menuId 排除的菜单ID
     * @return 数量
     */
    @Select("SELECT COUNT(1) FROM sys_menu WHERE menu_name = #{menuName} AND parent_id = #{parentId} " +
            "AND (#{menuId} IS NULL OR menu_id != #{menuId})")
    int checkMenuNameExists(@Param("menuName") String menuName, 
                           @Param("parentId") Long parentId, 
                           @Param("menuId") Long menuId);

    /**
     * 分页查询菜单列表
     * 
     * @param offset 偏移量
     * @param limit 限制数量
     * @param sysMenu 查询条件
     * @return 菜单列表
     */
    @Select("<script>" +
            "SELECT * FROM sys_menu " +
            "WHERE 1=1 " +
            "<if test='sysMenu.menuName != null and sysMenu.menuName != \"\"'>" +
            "  AND menu_name LIKE CONCAT('%', #{sysMenu.menuName}, '%') " +
            "</if>" +
            "<if test='sysMenu.status != null and sysMenu.status != \"\"'>" +
            "  AND status = #{sysMenu.status} " +
            "</if>" +
            "<if test='sysMenu.menuType != null and sysMenu.menuType != \"\"'>" +
            "  AND menu_type = #{sysMenu.menuType} " +
            "</if>" +
            "<if test='sysMenu.system != null'>" +
            "  AND system = #{sysMenu.system} " +
            "</if>" +
            "ORDER BY parent_id, order_num " +
            "LIMIT #{offset}, #{limit}" +
            "</script>")
    List<SysMenu> selectMenuList(@Param("offset") int offset, 
                                @Param("limit") int limit, 
                                @Param("sysMenu") SysMenu sysMenu);

    /**
     * 统计菜单数量
     * 
     * @param sysMenu 查询条件
     * @return 数量
     */
    @Select("<script>" +
            "SELECT COUNT(1) FROM sys_menu " +
            "WHERE 1=1 " +
            "<if test='sysMenu.menuName != null and sysMenu.menuName != \"\"'>" +
            "  AND menu_name LIKE CONCAT('%', #{sysMenu.menuName}, '%') " +
            "</if>" +
            "<if test='sysMenu.status != null and sysMenu.status != \"\"'>" +
            "  AND status = #{sysMenu.status} " +
            "</if>" +
            "<if test='sysMenu.menuType != null and sysMenu.menuType != \"\"'>" +
            "  AND menu_type = #{sysMenu.menuType} " +
            "</if>" +
            "<if test='sysMenu.system != null'>" +
            "  AND system = #{sysMenu.system} " +
            "</if>" +
            "</script>")
    int countMenus(@Param("sysMenu") SysMenu sysMenu);

    /**
     * 根据用户ID查询用户可访问的菜单
     * 
     * @param userId 用户ID
     * @return 菜单列表
     */
    @Select("SELECT DISTINCT m.* FROM sys_menu m " +
            "INNER JOIN sys_permission p ON m.menu_id = p.resource_id " +
            "INNER JOIN sys_user_role ur ON p.role_id = ur.role_id " +
            "WHERE ur.user_id = #{userId} AND m.status = '0' AND m.visible = '0' " +
            "ORDER BY m.parent_id, m.order_num")
    List<SysMenu> selectMenusByUserId(@Param("userId") Long userId);

    /**
     * 构建菜单树
     * 
     * @param parentId 父菜单ID
     * @return 菜单树
     */
    @Select("SELECT * FROM sys_menu WHERE parent_id = #{parentId} AND status = '0' ORDER BY order_num")
    List<SysMenu> selectMenuTree(@Param("parentId") Long parentId);
}
